package com.lancoo.answer.view.result

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lancoo.answer.R
import com.lancoo.answer.model.bean.CognitiveBean
import com.lancoo.answer.util.DensityUtils
import com.lancoo.answer.util.DoubleUtils
import com.lancoo.answer.util.MethodUtils
import com.lancoo.answer.util.SpacesItemDecoration
import com.lancoo.answer.view.base.BaseRecyclerAdapter
import com.lancoo.answer.view.base.BaseRecyclerViewHolder
import com.lancoo.answer.view.result.ResultAnswerSheetChildAdapter.OnClickChildListener

class ResultAnswerSheetParentAdapter(
    val activity: FragmentActivity,
    val cognitiveBean: CognitiveBean
) : BaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
            .inflate(R.layout.ev_adapter_answersheet_parent, parent,
                false)
    }

    override fun onBindViewHolder(holder: BaseRecyclerViewHolder, position: Int) {

        val mTvTypeInfor = holder.convertView.findViewById<TextView>(R.id.TvTypeInfor)
        val mRvAnswerSheetChild = holder.convertView.findViewById<RecyclerView>(R.id.RvAnswerSheetChild)

        val typeIndex = cognitiveBean.TypeList?.get(position)?.TypeIndex
        val strTypeIndex = MethodUtils.numberToChinese(typeIndex!!)
        var genreName = cognitiveBean.TypeList?.get(position)?.GenreName
        val typeName = cognitiveBean.TypeList?.get(position)?.TypeName
        if (genreName!!.isEmpty()) {
            genreName = typeName
        }
        var evalScore = cognitiveBean.TypeList?.get(position)?.EvalScore
        var totalScore = cognitiveBean.TypeList?.get(position)?.TotalScore
        totalScore = DoubleUtils.killlingDouble(totalScore!!.toDouble())
        //显示分数及准确率颜色
        if (evalScore == "--") {
            mTvTypeInfor.text =
                Html.fromHtml(
                    activity.resources.getString(
                        R.string.ev_result_ques_title_blue,
                        strTypeIndex, genreName, evalScore, totalScore
                    )
                )
        } else {
            evalScore = DoubleUtils.killlingDouble(evalScore!!.toDouble())
            val correctRate =
                MethodUtils.getBusiness(evalScore!!.toDouble(), totalScore!!.toDouble())
            if (correctRate >= 0.6) {
                mTvTypeInfor.text =
                    Html.fromHtml(
                        activity.resources.getString(
                            R.string.ev_result_ques_title_blue,
                            strTypeIndex, genreName, evalScore, totalScore
                        )
                    )
            } else {
                mTvTypeInfor.text =
                    Html.fromHtml(
                        activity.resources.getString(
                            R.string.ev_result_ques_title_blue,
                            strTypeIndex, genreName, evalScore, totalScore
                        )
                    )
            }
        }

        /*******************************************************************************************/
//        val newSortIndex = cognitiveBean.TypeList?.get(position)?.QuesList!![0].SortIndexList!![0].NewSortIndex
//        if (newSortIndex!!.contains("-")) {
//            val space = DensityUtils.dp2px(activity,8f)
//            mRvAnswerSheetChild.addItemDecoration(SpacesItemDecoration(space))
//            mRvAnswerSheetChild.layoutManager = GridLayoutManager(activity, 4)
//        } else {
//            val space = DensityUtils.dp2px(activity,0f)
//            mRvAnswerSheetChild.addItemDecoration(SpacesItemDecoration(space))
//            mRvAnswerSheetChild.layoutManager = GridLayoutManager(activity, 6)
//        }
        /*******************************************************************************************/

        val adapterChild = ResultAnswerSheetChildAdapter(
            activity, cognitiveBean, position
        )
        holder.convertView.findViewById<RecyclerView>(R.id.RvAnswerSheetChild).adapter = adapterChild

        adapterChild.setClickChildListener(object : OnClickChildListener {
            override fun onClick(
                index_A: Int,
                index_B: Int,
                index_C: Int
            ) {
                clickParentListener!!.onClick(index_A, index_B, index_C)
            }
        })


    }

    override fun getItemCount(): Int {
        return cognitiveBean.TypeList!!.size
    }

    interface OnClickParentListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickParentListener: OnClickParentListener? = null

    fun setClickParentListener(clickParentListener: OnClickParentListener?) {
        this.clickParentListener = clickParentListener
    }

}